package com.jinghang.wayofrich.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.jinghang.wayofrich.R;
import com.jinghang.wayofrich.beans.IndexArticle;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

public class IndexListAdapter extends BaseAdapter {
    private List<IndexArticle> listData;
    private LayoutInflater inflater;

    public IndexListAdapter(List<IndexArticle> listData, Context context) {
        this.listData = listData;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return listData.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageLoader imageLoader= ImageLoader.getInstance();
        ViewHolderList holer = null;
        View view = inflater.inflate(R.layout.fragment_index_articles, null);
        if (holer == null) {
            holer = new ViewHolderList();
            holer.title = (TextView) view.findViewById(R.id.articleTitle);
            holer.createTime = (TextView) view.findViewById(R.id.articleDate);
            holer.imgUrl = (ImageView) view.findViewById(R.id.articleImg);
            view.setTag(holer);
        } else {
            holer = (ViewHolderList) view.getTag();
        }
        holer.createTime.setText(listData.get(position).getCreateTime());
        holer.title.setText(listData.get(position).getTitle());
        imageLoader.displayImage(listData.get(position).getImgUrl(), holer.imgUrl);
        return view;
    }
    //  适配器中的GridView缓存类
    class ViewHolderList {
        TextView title;
        TextView createTime;
        ImageView imgUrl;
    }
}
